package com.ruoyi.framework.reptile.webmagic.jingcai.rank.cba;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.constant.JingCaiWebMagicConstants;
import com.ruoyi.framework.reptile.webmagic.jingcai.AbstractJingCaiPageProcessor;
import org.apache.commons.collections4.CollectionUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author dzl
 * @Description 新浪cba排名
 * @date 2024年07月09日 21:04:00
 */
public class SinaCbaRankPageProcessor extends AbstractJingCaiPageProcessor implements PageProcessor {

    // 部分一：抓取网站的相关配置，包括编码、抓取间隔、重试次数等
    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);

    @Override
    public Site getSite() {
        return site;
    }

    // process是定制爬虫逻辑的核心接口，在这里编写抽取逻辑
    @Override
    public void process(Page page) {
        String season = "";
        Document doc = page.getHtml().getDocument();
        Elements tables = doc.select("table");
        Elements seasons = doc.select("select");
        if (CollectionUtils.isNotEmpty(seasons)) {
            season = seasons.get(0).text().split(" ")[0];
        }
        List<CbaRankResult> cbaRankResultList = new ArrayList<>();
        for (Element table : tables) {
            Elements rows = table.select("tr");
            rows.remove(0);
            for (Element row : rows) {
                CbaRankResult cbaRankResult = new CbaRankResult();
                cbaRankResult.setSeason(season);
                cbaRankResultList.add(cbaRankResult);
                Elements cells = row.select("td");
                for (int c = 0; c < cells.size() ; c++) {
                    if (c == 0) {
                        cbaRankResult.setRank(Integer.valueOf(cells.get(c).text()));
                    }
                    if (c == 1) {
                        cbaRankResult.setTeam_name(cells.get(c).text());
                    }

                    if (c == 2) {
                        cbaRankResult.setWins(Integer.valueOf(cells.get(c).text()));
                    }

                    if (c == 3) {
                        cbaRankResult.setLosses(Integer.valueOf(cells.get(c).text()));
                    }

                    if (c == 5) {
                        List<Integer> winsAndLoss = WinsAndLossUtil.getWinsAndLoss(cells.get(c).text());
                        cbaRankResult.setHome_wins(winsAndLoss.get(0));
                        cbaRankResult.setHome_losses(winsAndLoss.get(1));
                    }

                    if (c == 6) {
                        List<Integer> winsAndLoss = WinsAndLossUtil.getWinsAndLoss(cells.get(c).text());
                        cbaRankResult.setRoad_wins(winsAndLoss.get(0));
                        cbaRankResult.setRoad_losses(winsAndLoss.get(1));
                    }

                    if (c == 7) {
                        cbaRankResult.setPoints_for(new BigDecimal(cells.get(c).text()));
                    }

                    if (c == 8) {
                        cbaRankResult.setPoints_against(new BigDecimal(cells.get(c).text()));
                    }
                }
            }
        }
        page.putField("jcData", JSON.toJSONString(cbaRankResultList));
    }

    @Override
    protected void start() {
        Spider.create(new SinaCbaRankPageProcessor())
                .addUrl(JingCaiWebMagicConstants.JINGCAI_SINA_CBA_RANK_URL)
                .addPipeline(new SinaCbaRankPipeline())
                //开启5个线程抓取
                .thread(5)
                //启动爬虫
                .run();
    }
}
